package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Forwarder;
import com.by.bean.ForwarderInquirySheet;
import com.by.bean.ForwarderProduct;
import com.by.bean.ForwarderQuoteList;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.mapper.CustomerMapper;
import com.by.mapper.ForwarderProductMapper;
import com.by.mapper.ForwarderQuoteListMapper;
import com.by.query.ForwarderQuery;
import com.by.query.FreightInquiryQuery;
import com.by.service.impl.ForwarderProductService;
import com.by.service.impl.ForwarderQuoteListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "后台货代询价")
@RequestMapping("/api")
public class ForwarderQuoteListController extends BaseController {

    @Autowired
    private ForwarderQuoteListService forwarderQuoteListService;

    @Autowired
    private ForwarderProductService forwarderProductService;

    @Autowired
    private ForwarderQuoteListMapper forwarderQuoteListMapper;
    @Autowired
    CustomerMapper customerMapper;


    @PostMapping("/forwarderQuoteList")
    @ApiOperation("添加询价清单及要求&产品信息")
    public ResultBean save(@RequestBody ForwarderQuoteList forwarderQuoteList) {
        int rs = forwarderQuoteListService.save(forwarderQuoteList);
        return saveResult(rs);
    }

    @ApiOperation("分页查询货代信息")
    @PostMapping("/forwarderQuoteList/list")
    public ResultBean<PageBean<List<ForwarderInquirySheet>>> query(@RequestBody FreightInquiryQuery forwarderQuery) {
        PageBean bean = forwarderQuoteListService.queryByMap(BeanUtil.beanToMap(forwarderQuery));
        return success("货代询价列表", bean);
    }

    @ApiOperation("分页查产品信息")
    @PostMapping("/forwarderProduct/list")
    public ResultBean<PageBean<List<ForwarderProduct>>> querySave(@RequestBody ForwarderProduct forwarderQuery) {
        PageBean bean = forwarderProductService.queryByMap(BeanUtil.beanToMap(forwarderQuery));
        return success("货代产品列表", bean);
    }

    @ApiOperation("查询货代询价表信息")
    @GetMapping("/forwarderQuoteList/get/{quote_coding}")
    public ResultBean get(@PathVariable String quote_coding) {
        // 修改返回类型为 ForwarderQuoteList
        ForwarderQuoteList forwarderQuoteList = forwarderQuoteListMapper.forwarderQuoteList(quote_coding);
        return success("获取成功", forwarderQuoteList);
    }
    @ApiOperation("修改产品信息")
    @PutMapping("/forwarderProduct/update")
    public ResultBean update(@RequestBody ForwarderProduct forwarder){
        int rs = forwarderProductService.update(forwarder);
        return updateResult(rs);
    }

    @ApiOperation("修改询价单信息")
    @PutMapping("/forwarderQuoteList/update")
    public ResultBean updateList(@RequestBody ForwarderQuoteList forwarder){
        int rs = forwarderQuoteListService.update(forwarder);
        return updateResult(rs);
    }

    @ApiOperation("货代添加时查询客户产品信息")
    @GetMapping("/forwarderInquiry/detail/{customer_name}/{page}/{pageSize}")
    public ResultBean queryCustomerInquiryDetailsByName(@PathVariable String customer_name,
                          @PathVariable int page,
                          @PathVariable int pageSize) {
        // 使用 PageHelper 分页

        PageHelper.startPage(page, pageSize);
        List<Map> list = customerMapper.queryCustomerInquiryDetailsByName(customer_name);
        PageInfo<Map> info = new PageInfo<>(list);

        // 返回分页信息
        PageBean pageBean = new PageBean(page, pageSize, info.getTotal(), info.getList());
        return success("查询客户产品信息成功", pageBean);
    }

}

